{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 261,
   "metadata": {},
   "outputs": [],
   "source": [
    "import math\n",
    "import pandas as pd\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.neighbors import DistanceMetric\n",
    "import numpy as np\n",
    "from sklearn import metrics\n",
    "from matplotlib import pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 262,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def euc_distance(a, b):\n",
    "    dist = DistanceMetric.get_metric('euclidean')\n",
    "    ndarray = dist.pairwise([a, b])\n",
    "    distance = ndarray[0][-1]\n",
    "    return distance"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 263,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def closest(trainSet, trainLabels, row):\n",
    "    dists = [euc_distance(row, item) for item in trainSet]\n",
    "    nei = dists.index(min(dists))\n",
    "    return trainLabels[nei]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 264,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def predict(trainSet, trainLabels, to_classify, k):\n",
    "    predictions = []\n",
    "    for row in to_classify:\n",
    "        labels = kClosest(trainSet, trainLabels, row, k)\n",
    "        predictions.append(max(set(labels), key=labels.count))\n",
    "    return predictions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 265,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def kClosest(trainSet, trainLabels, row, k):\n",
    "    labels = list(trainLabels)\n",
    "    dists = [euc_distance(row, item) for item in trainSet]\n",
    "    kclosest = []\n",
    "    for i in range(k):\n",
    "        nei = dists.index(min(dists))\n",
    "        kclosest.append(labels[nei])\n",
    "        del dists[nei]\n",
    "        del labels[nei]\n",
    "    return kclosest"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 266,
   "metadata": {},
   "outputs": [],
   "source": [
    "dataSet = pd.read_csv('train.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 267,
   "metadata": {},
   "outputs": [],
   "source": [
    "dataSet.drop(['Ticket'], axis=1,inplace=True)\n",
    "dataSet.drop(['Name'], axis=1,inplace=True)\n",
    "dataSet.drop(['Cabin'], axis=1,inplace=True)\n",
    "dataSet.drop(['PassengerId'], axis=1,inplace=True)\n",
    "\n",
    "dataSet[\"Age\"] = dataSet.Age.fillna(dataSet.Age.mean())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 268,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.preprocessing import LabelEncoder\n",
    "\n",
    "sexEncoder = LabelEncoder().fit(dataSet.Sex)\n",
    "dataSet['Sex'] = sexEncoder.transform(dataSet.Sex)\n",
    "\n",
    "embarkedEncoder = LabelEncoder().fit(dataSet.Embarked.fillna(\"None\"))\n",
    "dataSet[\"Embarked\"] = embarkedEncoder.transform(dataSet.Embarked.fillna(\"None\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 269,
   "metadata": {},
   "outputs": [],
   "source": [
    "dataColumns = ['Pclass', 'Sex', 'Age', 'SibSp', 'Parch', 'Fare', 'Embarked']\n",
    "X = dataSet[dataColumns]\n",
    "\n",
    "targetColumn = ['Survived']\n",
    "y = dataSet[targetColumn]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 270,
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.25)\n",
    "\n",
    "X_train = np.array(X_train)\n",
    "X_test = np.array(X_test)\n",
    "y_train = np.array(y_train)\n",
    "y_test = np.array(y_test)\n",
    "\n",
    "X_train_new = []\n",
    "for i in range(len(X_train)):\n",
    "    X_train_new.append(list(X_train[i]))\n",
    "    \n",
    "X_test_new = []\n",
    "for i in range(len(X_test)):\n",
    "    X_test_new.append(list(X_test[i]))\n",
    "\n",
    "y_train_new = []\n",
    "for i in range(len(y_train)):\n",
    "    y_train_new.append(y_train[i][0])\n",
    "    \n",
    "y_test_new = []\n",
    "for i in range(len(y_test)):\n",
    "    y_test_new.append(y_test[i][0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 271,
   "metadata": {},
   "outputs": [],
   "source": [
    "def accuracies(kLimit, kStep, real):\n",
    "    accuracies = []\n",
    "    for i in range(1, kLimit, kStep):\n",
    "        predictions = predict(X_train_new, y_train_new, X_test_new, i)\n",
    "        accuracy = metrics.accuracy_score(predictions, real)\n",
    "        accuracies.append(accuracy)\n",
    "    return accuracies"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 272,
   "metadata": {},
   "outputs": [],
   "source": [
    "acc = accuracies(30, 4, y_test_new)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 273,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYwAAAEKCAYAAAAB0GKPAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAFqxJREFUeJzt3X+w3XV95/HnqwmUBB0CJduRgAZ3KN20W4PeomutWLVL\nWEuB7Q+JtVW3LaUrljrbrKQ7O7qd2dmdjWvrzlJZtFi7teAPYmA6HQJai9VtXW5MlACmZlAkASUu\nRoWJhYT3/nG+Vw+Xm9zPTe7Juefc52Pmjuf7+X6+976/fm/ui+/nc873k6pCkqTZ/MCwC5AkjQYD\nQ5LUxMCQJDUxMCRJTQwMSVITA0OS1MTAkCQ1MTAkSU0GGhhJ1iXZlWR3kmtm2L8hyY7ua2eSQ0lO\n6/a9Nck9XfuNSU4aZK2SpCPLoD7pnWQJ8A/AzwJ7gLuA9VV172H6Xwy8tapemWQV8GlgTVUdSPJh\n4K+q6k+P9DNPP/30Wr169TyehSSNt23btn2jqla29F06wDrOB3ZX1f0ASW4CLgFmDAxgPXDjtNqW\nJXkSWA48NNsPXL16NZOTk8dUtCQtJkkeaO07yCGpVcCDfdt7urZnSLIcWAfcDFBVe4F3Al8FHga+\nVVW3H+bYK5JMJpnct2/fPJYvSeq3UCa9LwY+U1WPAiQ5ld7d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1F5C7+bgw1X1B0l+iKO8XmMZGJKk+TeOQ1KSpAEwMCRJTQwMSVITA0OS1MTAkCQ1MTCkRkmuTPJr\ns/R5Y5L/eZh9j83ULo2KpcMuQBoVVXXdsGuYLsnSqjo47Dq0OHiHoUUryeok9yV5b7dewO1JliX5\np0lu6x7w+LdJfrTr/44kv9e9/snuoXQ7kmzqX/sCOKM7/kvdI9v7f+Yfdj/rE0lWdm1rk/x99/0+\nNvWQuyR/k2Sie31692j7qbuYW5P8NfCJJM9J8qmulp1Jfnrg/+dpUTIwtNidA1xbVT8G7Ad+gd6a\nx2+pqhcBvwf88QzHvZ/eJ3/XAoem7VsLvBb458Brk0w92+xkYLL7WXfS+wQ4wJ8Bb6uqn6D3aeO3\nM7sXAr9YVRcArwO2drW8ANjRcLw0Zw5JabH7clVN/YHdBqwGXgp8pPeIIQB+sP+A7vk8z66qv+ua\n/gL4ub4un6iqb3V97wWeR++R+08BH+r6/DmwOckpwIqqurNr/wDwkYa67+h7nMNdwA3dA/S29J2P\nNK+8w9Bi9499rw8BpwH7q2pt39c/O8bvebj/MJvtuTwH+f6/0ZOm7Xv8e9+kt2DTy+k9lflPZ5uY\nl46WgSE93beBLyf5Jeg9yTTJC/o7dI+K/k6SF3dNlzd+7x8AfrF7/Trg092dyDf75h1+ld5wFfQe\nePei7vXUcc+Q5HnA16vqvcD76A1XSfPOwJCe6VeAX0/yeeAeZl7i99eB93armZ0MfKvh+z5ObxGb\nncArgT/o2t8AbEryBXrzH1Pt7wR+O8l2ek++PZxXAJ/v+r0WeHdDLdKc+bRa6SgkedbUeslJrgGe\nU1VXD7ksaaCc9JaOzmuSbKT3b+gB4I3DLUcaPO8wJElNnMOQJDUxMCRJTQwMSVITA0OS1MTAkCQ1\nMTAkSU3+P8Mc6ch9TVYiAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7f1e588aa828>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "neighbours = [i for i in range(1, 30, 4)]\n",
    "\n",
    "plt.scatter(neighbours, acc)\n",
    "plt.xlabel('neighbours')\n",
    "plt.ylabel('accuracy')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 274,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Melhor k: 9\n",
      "Precisão do melhor k: 0.7623318385650224\n",
      "\n",
      "              precision    recall  f1-score   support\n",
      "\n",
      "    Survived       0.79      0.83      0.81       138\n",
      "Not Survived       0.71      0.65      0.67        85\n",
      "\n",
      " avg / total       0.76      0.76      0.76       223\n",
      "\n"
     ]
    }
   ],
   "source": [
    "bestK = neighbours[np.argmax(acc)]\n",
    "bestAcc = max(acc)\n",
    "\n",
    "print('Melhor k: {0}'.format(bestK))\n",
    "print('Precisão do melhor k: {0}\\n'.format(bestAcc))\n",
    "\n",
    "predictionsBestK = predict(X_train_new, y_train_new, X_test_new, bestK)\n",
    "print(metrics.classification_report(y_test_new, predictionsBestK, target_names=['Survived', 'Not Survived']))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
